package com.minefinder;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.AssetManager;
import android.graphics.Color;
import android.media.AudioManager;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class ScoreActivity extends Activity {
	static final String SCORE_FILE_POSTFIX = "_score.txt";
	static final int SCORE_MAX_COUNT = 10;
	static final double INIT_SCORE = 9999.0;

	AssetManager assetManager;
	List<Double> scoreList;

	TableLayout tlScore;
	
	Button btnResetScore;

	String dirPath;
	String level;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_score);

		// set volume
		setVolumeControlStream(AudioManager.STREAM_MUSIC);
		
		Intent intent = getIntent();
		this.level = intent.getStringExtra(OptionsActivity.LEVEL_STRING);

		this.assetManager = this.getAssets();
		this.scoreList = new ArrayList<Double>();
		this.dirPath = getFilesDir().getAbsolutePath();
		
		this.tlScore = (TableLayout) findViewById(R.id.tl_score);
		
		this.btnResetScore = (Button) findViewById(R.id.btn_resetscore);
		this.btnResetScore.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				
				AlertDialog.Builder dlg = new AlertDialog.Builder(v.getContext());
				dlg.setTitle("Warning!!");
				dlg.setMessage("All score will reset! continue?");
				
				dlg.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						resetScore();
					}
				});
				
				dlg.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						dialog.cancel();
					}
				});
				
				dlg.show();
			}
		});
		
		this.readScore();

		this.initScoreBoard();
	}
	
	public void readScore() {
		File scoreDir = new File(this.dirPath);
		File scoreFile = new File(this.dirPath + "/" + this.level + SCORE_FILE_POSTFIX);
		
		if (!scoreDir.exists()) {
			scoreDir.mkdirs();
		}
		
		if (!scoreFile.exists()) {
			try {
				scoreFile.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
			}
			
			this.resetScore();
		} else {		
			try {
				FileInputStream fis = new FileInputStream(scoreFile);
				BufferedReader br = new BufferedReader(new InputStreamReader(fis));
				
				String line = br.readLine();
				while (line != null) {
					double score = Double.parseDouble(line);
					this.scoreList.add(score);
					
					line = br.readLine();
				}
				
				br.close();
				fis.close();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
				
			Collections.sort(this.scoreList);
		}
	}

	public void initScoreBoard() {
		this.tlScore.removeAllViews();
		
		for (Double score : this.scoreList) {
			TableRow tr = new TableRow(this);
			tr.setGravity(Gravity.CENTER);
			
			TextView tv = new TextView(this);	
			tv.setGravity(Gravity.CENTER);
			tv.setTextColor(Color.WHITE);
			tv.setText("" + score);
			
			tr.addView(tv);			
			this.tlScore.addView(tr);
			
			//Log.i("info", "score: " + score);
		}
	}

	public void resetScore() {
		this.scoreList = new ArrayList<Double>();
		
		for (int i = 0; i < SCORE_MAX_COUNT; i++) {
			this.scoreList.add(INIT_SCORE);
		}		

		File scoreFile = new File(this.dirPath + "/" + this.level + SCORE_FILE_POSTFIX);
		try {
			FileOutputStream fos = new FileOutputStream(scoreFile);
			BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));
			
			String scoreStr = "" + INIT_SCORE;
			for (int i = 0; i < SCORE_MAX_COUNT; i++) {
				bw.write(scoreStr);
				
				if (i != ScoreActivity.SCORE_MAX_COUNT - 1) {
					bw.newLine();
				}
			}
			
			bw.close();			
			fos.close();
			
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		this.initScoreBoard();
	}
}
